﻿using Sasc.PV.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sasc.PV.DAL
{
    public class ZhuDingGongXuTarget : ICheJianGongXuTarget
    {
        /// <summary>
        /// Required
        /// </summary>
        public string GanGuoBianHao { get; set; }
        /// <summary>
        /// Optional
        /// </summary>
        public string XiaoDingBianHao { get; set; }

        public GongXuTargetDataType GanGuoBianHaoType { get; set; }

        public GongXuTargetDataType XiaoDingBianHaoType { get; set; }

        /// <summary>
        /// 注意，这里会要求 GongXuModel的GanGuoBianHao和XiaoDingBianHao的属性名匹配
        /// </summary>
        /// <returns></returns>
        public string GenerateExistWhereSQL()
        {
            var sqlBuilder = new StringBuilder();

            if (GanGuoBianHaoType == GongXuTargetDataType.Required)
            {
                sqlBuilder.AppendFormat(" GanGuoBianHao='{0}' ", GanGuoBianHao);
            }
            else if (GanGuoBianHaoType == GongXuTargetDataType.Optional)
            {
                if (GanGuoBianHao == null)
                {
                    sqlBuilder.Append(" GanGuoBianHao IS NULL ");
                }
                else
                {
                    sqlBuilder.AppendFormat(" GanGuoBianHao='{0}' ", GanGuoBianHao);
                }
            }

            var appendAnd = sqlBuilder.Length > 0 ? "AND" : "";

            if (XiaoDingBianHaoType == GongXuTargetDataType.Required)
            {
                sqlBuilder.AppendFormat(" {1} XiaoDingBianHao='{0}' ", XiaoDingBianHao, appendAnd);
            }
            else if (XiaoDingBianHaoType == GongXuTargetDataType.Optional)
            {
                if (XiaoDingBianHao == null)
                {
                    sqlBuilder.Append(" {1}  XiaoDingBianHao IS NULL ");
                }
                else
                {
                    sqlBuilder.AppendFormat(" {1}  XiaoDingBianHao='{0}' ", XiaoDingBianHao, appendAnd);
                }
            }

            return sqlBuilder.ToString();
        }

        public override string ToString()
        {
            var stringBuilder =new StringBuilder();
            if (!string.IsNullOrEmpty(GanGuoBianHao))
                stringBuilder.AppendFormat("坩埚：{0} ", GanGuoBianHao);
            if (!string.IsNullOrEmpty(XiaoDingBianHao))
                stringBuilder.AppendFormat("小锭：{0} ", XiaoDingBianHao);
            return stringBuilder.ToString();
        }

        public override int GetHashCode()
        {
            return string.Format("{0}|&{1}", GanGuoBianHao, XiaoDingBianHao).GetHashCode();
        }
    }
}
